#!/usr/bin/env python3

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

img = cv.imread('../pic/7.png')
height, width = img.shape[:2]
print(img.shape[:2])
cv.imshow("window", img)

# 缩放方法一
res_size = cv.resize(img, None, fx=2, fy=2, interpolation=cv.INTER_CUBIC)
cv.imshow("resize_window", res_size)

# 缩放方法二
res_size2 = cv.resize(img, (2 * width, 2 * height), interpolation=cv.INTER_CUBIC)
cv.imshow("resize_window2", res_size2)
cv.waitKey(0)
cv.destroyAllWindows()

# 平移 向右移动，像上x，y  100，-50  正负代表方向
M_shift = np.float32([[1, 0, 100], [0, 1, -50]])
shift_img = cv.warpAffine(img, M_shift, (width, height))

# 旋转
center = (img.shape[1] / 2, img.shape[0] / 2)
angle = 90
scale = 0.5
# 仍是2*3的矩阵
M_rotate = cv.getRotationMatrix2D(center, angle, scale)
rotate_img = cv.warpAffine(img, M_rotate, (width, height))

print(M_rotate)

cv.imshow("window", img)
cv.imshow("shift_img", shift_img)
cv.imshow("rotate_img", rotate_img)
cv.waitKey(0)
cv.destroyAllWindows()

# 仿射变换 需要提供原图中的三个点  原图像三个点在变换后相应的坐标
rows, cols, ch = img.shape
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[10, 100], [200, 50], [100, 250]])
M = cv.getAffineTransform(pts1, pts2)
dst = cv.warpAffine(img, M, (cols, rows))
plt.subplot(121), plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB)), plt.title('Input')
plt.subplot(122), plt.imshow(cv.cvtColor(dst, cv.COLOR_BGR2RGB)), plt.title('Output')
plt.show()

# # 透视变换 转换视角
# rows, cols, ch = img.shape
# pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])
# pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
# M = cv.getPerspectiveTransform(pts1, pts2)
# dst = cv.warpPerspective(img, M, (rows, cols))
# plt.subplot(121), plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB)), plt.title('Input')
# plt.subplot(122), plt.imshow(cv.cvtColor(dst, cv.COLOR_BGR2RGB)), plt.title('Output')
# plt.show()

cv.destroyAllWindows()
